package com.jia.leetCode;

/**
 * @program: Leetcode
 * @description:  https://leetcode-cn.com/contest/biweekly-contest-17/problems/sum-of-nodes-with-even-valued-grandparent/
 * @author: STU756
 * @create: 2020-08-21 20:37
 */
public class Pro1315 {
    public int sumEvenGrandparent(TreeNode root) {
        if(root == null) return 0;
        if(root.left== null && root.right == null) return 0;
        return help(root,root.left) + help(root, root.right);
    }
    private int help(TreeNode gf, TreeNode f) {
        if(f == null) return 0;
        int ans = 0;
        if(gf.val % 2 == 0) {

            if(f.left!=null){
                ans += f.left.val;
            }
            if(f.right != null) {
                ans += f.right.val;
            }
        }
        return ans + help(f, f.left) + help(f, f.right);
    }
}
